package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * 课程服务实现类
 * @author daygods
 * @since 2025-07-16
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Override
    public String saveCourseInfo(EduCourse eduCourse) {
        // 向课程表添加课程基本信息
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0) {
            throw new RuntimeException("添加课程信息失败");
        }
        
        // 获取添加之后课程id
        String cid = eduCourse.getId();
        return cid;
    }

    @Override
    public Page<EduCourse> getCoursePage(long current, long limit) {
        Page<EduCourse> pageParam = new Page<>(current, limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        
        baseMapper.selectPage(pageParam, wrapper);
        return pageParam;
    }

    @Override
    public boolean removeCourse(String courseId) {
        // 删除课程
        int result = baseMapper.deleteById(courseId);
        return result > 0;
    }
}